
export interface BaseResponse<T = any> {
	code : number;
	msg : string;
	data : T;
}

/**
 * 用于封装请求路径和缓存的基础模型类
 * T 表示数据结构类型
 */
export default class BaseService {
	protected endpoint : string;

	constructor(endpoint : string = '') {
		this.endpoint = endpoint;
	}

	/**
	 * 静态解析响应数据
	 */
	public static parseResponse<T = any>(data : string | AnyObject | ArrayBuffer) : BaseResponse<T> {
		let response : BaseResponse<T>;

		if (typeof data === 'string') {
			try {
				response = JSON.parse(data) as BaseResponse<T>;
			} catch (error) {
				console.error('响应 JSON 解析失败:', error);
				throw new Error('响应数据格式错误');
			}
		} else if (typeof data === 'object') {
			response = data as BaseResponse<T>;
		} else {
			throw new Error('不支持的响应类型');
		}

		if (typeof response.code !== 'number' || typeof response.msg !== 'string') {
			throw new Error('响应结构非法');
		}

		return response;
	}
}